<!--
 * @Description: 商品详情页面组件
 * @Author: hai-27
 * @Date: 2020-02-16 20:20:26
 * @LastEditors: hai-27
 * @LastEditTime: 2020-03-07 21:59:26
 -->
<template>
  <div id="details">
    <!-- 头部 -->
    <div class="page-header">
      <div class="title">
        <p>{{productDetails.product_name}}</p>
        <div class="list">
          <!-- <ul>
            <li>
              <router-link to>概述</router-link>
            </li>
            <li>
              <router-link to>参数</router-link>
            </li>
            <li>
              <router-link to>用户评价</router-link>
            </li>
          </ul> -->
        </div>
      </div>
    </div>
    <!-- 头部END -->

    <!-- 主要内容 -->
    <div class="main">
      <el-row>

        <!-- 左侧商品轮播图 -->
        <div class="block">
          <el-carousel
            height="560px"
            v-if="productPicture.length>1"
          >
            <el-carousel-item
              v-for="item in productPicture"
              :key="item.id"
            >
              <img
                style="height:560px;width:560px"
                :src=" item.img"
              />
            </el-carousel-item>
          </el-carousel>
          <div v-if="productPicture.length==1">
            <img
              style="height:560px;"
              :src="$target + productPicture[0].product_picture"
              :alt="productPicture[0].intro"
            />
          </div>
        </div>
        <!-- 左侧商品轮播图END -->

        <!-- 右侧内容区 -->
        <div class="content">
          <h1 class="name">{{productDetails.softwareName}}</h1>
          <div class="intro  mt20"> 版本:{{productDetails.version}}</div>
          <div class="intro ">大小:{{productDetails.mbSize}}</div>
          <div class="intro">更新时间:{{productDetails.updateTime}}</div>

          <div class="price">
            <div>
              <span>评分: <el-rate
                  v-model="productDetails.avgScore"
                  disabled
                  show-score
                  text-color="#ff9900"
                >
                </el-rate>

              </span>
            </div>
            <!-- <span>{{productDetails.product_selling_price}}元</span> -->
            <!-- <span
              v-show="productDetails.product_price != productDetails.product_selling_price"
              class="del"
            >{{productDetails.product_price}}元</span> -->
          </div>
          <!-- <div class="pro-list">
            <span class="pro-name">{{productDetails.product_name}}</span>
            <span class="pro-price">
              <span>{{productDetails.product_selling_price}}元</span>
              <span
                v-show="productDetails.product_price != productDetails.product_selling_price"
                class="pro-del"
              >{{productDetails.product_price}}元</span>
            </span>
            <p class="price-sum">总计 : {{productDetails.product_selling_price}}元</p>
          </div> -->
          <!-- 内容区底部按钮 -->
          <div class="button">
            <!-- <el-button
              class="shop-cart"
              :disabled="dis"
              @click="addShoppingCart"
            >立即购买</el-button> -->
            <el-button
              class="like"
              @click="downLoad"
            >下载</el-button>
          </div>
          <!-- 内容区底部按钮END -->
          <div class="pro-policy">
          </div>
        </div>
        <!-- 右侧内容区END -->
      </el-row>

      <el-row class="mt20">
        <el-card
          class="box-card"
          shadow="never"
        >
          <div
            slot="header"
            class="clearfix"
          >
            <span>软件简介</span>
          </div>
          <div>
            {{productDetails.rjDetails}}
          </div>
        </el-card>

      </el-row>

      <el-row class="mt20 pb300">
        <el-card
          class="box-card"
          shadow="never"
        >
          <div
            slot="header"
            class="clearfix"
          >
            <span>用户评论</span>
          </div>
          <div
            class=""
            style="margin-bottom:20px"
          >
            <div>
              <span>评分:</span>
              <el-rate
                v-model="avgScore"
                text-color="#ff9900"
              >
              </el-rate>
            </div>
          </div>
          <div class="">
            <el-input
              placeholder="请输入内容"
              v-model="remark"
              class="input-with-select"
              type="textarea"
            >

            </el-input>

          </div>
          <div
            class=""
            style="text-align:right;margin-top:10px"
          >
            <el-button
              size="small"
              type="primary"
              @click="submit()"
            >
              提交</el-button>
          </div>
          <div class="comment">
            <div
              class="comment-item"
              v-for="(item, index) in evaluationList"
              :key="index"
            >

              <div class="name">{{item.username}}</div>
              <div class="">{{item.evaluationInfo}}</div>
            </div>
          </div>
        </el-card>

      </el-row>

    </div>
    <!-- 主要内容END -->

  </div>
</template>
<script>
import { mapActions } from "vuex";
import { api } from "../api";
export default {
  data() {
    return {
      dis: false, // 控制“加入购物车按钮是否可用”
      productID: "", // 商品id
      productDetails: "", // 商品详细信息
      productPicture: [], // 商品图片

      evaluationList: [],
      avgScore: 5,
      remark: "",
    };
  },
  // 通过路由获取商品id
  activated() {
    if (this.$route.query.productID != undefined) {
      this.productID = this.$route.query.productID;
    }
  },
  watch: {
    // 监听商品id的变化，请求后端获取商品数据
    productID: function (val) {
      this.getDetails(val);
      // this.getDetailsPicture(val);
    },
  },
  created() {
    console.log(this.$route);
    if (this.$route.query.productID != undefined) {
      this.productID = this.$route.query.productID;
    }
  },
  methods: {
    ...mapActions(["unshiftShoppingCart", "addShoppingCartNum"]),
    // 获取商品详细信息
    getDetails(val) {
      this.$axios
        .post(api.softDetails, {
          id: val,
        })
        .then((res) => {
          res.data.data.avgScore = Number(res.data.data.avgScore);
          this.productDetails = res.data.data;
          this.productPicture = [];
          this.productPicture.push({ img: this.productDetails.aLogo, id: 1 });
          this.productPicture.push({ img: this.productDetails.bLogo, id: 2 });
          this.productPicture.push({ img: this.productDetails.cLogo, id: 3 });
          console.log(this.productPicture);

          this.$axios
            .post(api.evaluationList, {
              current: 0,
              size: 200,
              softwareCode: this.productDetails.softwareCode, //软件代号
            })
            .then((res) => {
              console.log(res);
              this.evaluationList = res.data.data.records;
            })
            .catch((err) => {
              return Promise.reject(err);
            });
        })
        .catch((err) => {
          return Promise.reject(err);
        });
    },

    // 获取商品图片
    getDetailsPicture(val) {
      this.$axios
        .post("/api/product/getDetailsPicture", {
          productID: val,
        })
        .then((res) => {
          this.productPicture = res.data.ProductPicture;
        })
        .catch((err) => {
          return Promise.reject(err);
        });
    },
    // 加入购物车
    addShoppingCart() {
      // 判断是否登录,没有登录则显示登录组件
      if (!this.$store.getters.getUser) {
        this.$store.dispatch("setShowLogin", true);
        return;
      }
    },

    submit() {
      // 判断是否登录,没有登录则显示登录组件
      if (!this.$store.getters.getUser) {
        this.$store.dispatch("setShowLogin", true);
        return;
      }
      console.log(this.$store.state.user.user);
      this.$axios
        .post(api.addEvaluation, {
          softwareCode: this.productDetails.softwareCode, //软件代号
          rate: this.avgScore, //评分
          evaluationInfo: this.remark, //评价
          username: this.$store.state.user.user.username, //评价的用户
        })
        .then((res) => {
          console.log(res);
          this.remark = "";
          this.avgScore = 5;
          this.getDetails(this.productID);
        })
        .catch((err) => {
          return Promise.reject(err);
        });
    },
    downLoad() {
      this.$axios
        .post(api.downloadSoft, {
          id: this.productID,
        })
        .then((res) => {
          console.log(res);
          window.open(this.productDetails.softwareUrl);
        })
        .catch((err) => {
          return Promise.reject(err);
        });
    },
    addCollect() {
      // 判断是否登录,没有登录则显示登录组件
      // if (!this.$store.getters.getUser) {
      // this.$store.dispatch("setShowLogin", true);
      //   return;
      // }
      this.$axios
        .post("/api/user/collect/addCollect", {
          user_id: this.$store.getters.getUser.user_id,
          product_id: this.productID,
        })
        .then((res) => {
          if (res.data.code == "001") {
            // 添加收藏成功
            this.notifySucceed(res.data.msg);
          } else {
            // 添加收藏失败
            this.notifyError(res.data.msg);
          }
        })
        .catch((err) => {
          return Promise.reject(err);
        });
    },
  },
};
</script>
<style>
/* 头部CSS */
#details .page-header {
  height: 64px;
  margin-top: -20px;
  z-index: 4;
  background: #fff;
  border-bottom: 1px solid #e0e0e0;
  -webkit-box-shadow: 0px 5px 5px rgba(0, 0, 0, 0.07);
  box-shadow: 0px 5px 5px rgba(0, 0, 0, 0.07);
}
#details .page-header .title {
  width: 1225px;
  height: 64px;
  line-height: 64px;
  font-size: 18px;
  font-weight: 400;
  color: #212121;
  margin: 0 auto;
}
#details .page-header .title p {
  float: left;
}
#details .page-header .title .list {
  height: 64px;
  float: right;
}
#details .page-header .title .list li {
  float: left;
  margin-left: 20px;
}
#details .page-header .title .list li a {
  font-size: 14px;
  color: #616161;
}
#details .page-header .title .list li a:hover {
  font-size: 14px;
  color: #ff6700;
}
/* 头部CSS END */

/* 主要内容CSS */
#details .main {
  width: 1225px;
  height: 560px;
  padding-top: 30px;
  margin: 0 auto;
}
#details .main .block {
  float: left;
  width: 560px;
  height: 560px;
}
#details .el-carousel .el-carousel__indicator .el-carousel__button {
  background-color: rgba(163, 163, 163, 0.8);
}
#details .main .content {
  float: left;
  margin-left: 25px;
  width: 640px;
}
#details .main .content .name {
  height: 30px;
  line-height: 30px;
  font-size: 24px;
  font-weight: normal;
  color: #212121;
}
#details .main .content .intro {
  color: #b0b0b0;
  /* padding-top: 10px; */
}
#details .main .content .store {
  color: #ff6700;
  padding-top: 10px;
}
#details .main .content .price {
  display: block;
  font-size: 18px;
  color: #ff6700;
  border-bottom: 1px solid #e0e0e0;
  padding: 25px 0 25px;
}
#details .main .content .price .del {
  font-size: 14px;
  margin-left: 10px;
  color: #b0b0b0;
  text-decoration: line-through;
}
#details .main .content .pro-list {
  background: #f9f9fa;
  padding: 30px 60px;
  margin: 50px 0 50px;
}
#details .main .content .pro-list span {
  line-height: 30px;
  color: #616161;
}
#details .main .content .pro-list .pro-price {
  float: right;
}
#details .main .content .pro-list .pro-price .pro-del {
  margin-left: 10px;
  text-decoration: line-through;
}
#details .main .content .pro-list .price-sum {
  color: #ff6700;
  font-size: 24px;
  padding-top: 20px;
}
#details .main .content .button {
  height: 55px;
  margin: 10px 0 20px 0;
}
#details .main .content .button .el-button {
  float: left;
  height: 55px;
  font-size: 16px;
  color: #fff;
  border: none;
  text-align: center;
}
#details .main .content .button .shop-cart {
  width: 260px;
  background-color: #ff6700;
}
#details .main .content .button .shop-cart:hover {
  background-color: #f25807;
}

#details .main .content .button .like {
  width: 260px;
  margin-left: 40px;
  background-color: #ff6700;
}
#details .main .content .button .like:hover {
  background-color: #f25807;
}
#details .main .content .pro-policy li {
  float: left;
  margin-right: 20px;
  color: #b0b0b0;
}
/* 主要内容CSS END */
.mt20 {
  margin-top: 20px;
}
.pb300 {
  padding-bottom: 300px;
}
.flex {
  display: flex;
}
.border-r {
  border-right: 1px solid #252526;
  padding-right: 5px;
}
.comment-item {
  padding-bottom: 20px;
  padding-top: 5px;
  border-bottom: 1px solid #ccc;
}
.name {
  font-size: 12px;
  color: #333333;
  margin-bottom: 5px;
}
</style>